import datetime

from fastapi import APIRouter

from apps.allsubdb.paper_zhongguoqingnianbao.models import PaperZhongGuoQingNianBaoSaveMonth, ZhongGuoQingNianBaoSave2, \
    ZhongGuoQingNianBaoSave3, ZhongGuoQingNianBaoSave4, ZhongGuoQingNianBaoParseSave4
from apps.core.m_route import ContextIncludedRoute
from apps.core.return_info import InputInfoModel, ReturnInfo, SaveStepMongoModel, SUCCESS, FAILED
from apps.sql_app.mmongodb import MongoDBClient, Coll

router = APIRouter(route_class=ContextIncludedRoute)


@router.post("/step1/save_month")
async def parse_detail(input: InputInfoModel[PaperZhongGuoQingNianBaoSaveMonth]):
    """
    保存到大表
    :param input:
    :return:
    """
    return_info = ReturnInfo()
    product = "ZQB"
    types = "11"
    step_name = "step1"
    data = input.data
    name = input.data.periodMonth
    ids = "_".join([product, types, step_name, name])
    # step_info = MongoDBClient.db30_1.client.htmljson.step_info
    step_info = Coll().get_step_info()
    cjip = await step_info.find_one({"_id": ids})
    if cjip:
        dicts = SaveStepMongoModel[PaperZhongGuoQingNianBaoSaveMonth](
            step_info=PaperZhongGuoQingNianBaoSaveMonth()).parse_obj(
            cjip).dict()
        dicts["step_info"] = data.dict()
        dicts["update_time"] = str(datetime.datetime.now())
        save = SaveStepMongoModel[PaperZhongGuoQingNianBaoSaveMonth](
            step_info=PaperZhongGuoQingNianBaoSaveMonth()).parse_obj(dicts)
        result = await step_info.replace_one({'_id': ids}, save.dict(by_alias=True, exclude={"id"}))
    else:
        save = SaveStepMongoModel[PaperZhongGuoQingNianBaoSaveMonth](_id=ids, product=product, types=types,
                                                                     step_name=step_name,
                                                                     step_info=PaperZhongGuoQingNianBaoSaveMonth().parse_obj(
                                                                         data.dict()),
                                                                     update_time=str(datetime.datetime.now()),
                                                                     create_time=str(datetime.datetime.now())
                                                                     )
        result = await step_info.insert_one(save.dict(by_alias=True))
    return_info.status = SUCCESS
    return_info.msg_code = 200
    return_info.msg = "保存到mongodb成功"
    return_info.data = ""
    return return_info.todict()


@router.post("/step2/save_page")
async def parse_detail(input: InputInfoModel[ZhongGuoQingNianBaoSave2]):
    """
    保存到大表
    :param input:
    :return:
    """
    return_info = ReturnInfo()
    product = "ZQB"
    types = "11"
    step_name = "step2"
    data = input.data
    name = input.data.periodDate
    ids = "_".join([product, types, step_name, name])
    # step_info = MongoDBClient.db30_1.client.htmljson.step_info
    step_info = Coll().get_step_info()
    cjip = await step_info.find_one({"_id": ids})
    if cjip:
        dicts = SaveStepMongoModel[ZhongGuoQingNianBaoSave2](step_info=ZhongGuoQingNianBaoSave2()).parse_obj(
            cjip).dict()
        dicts["step_info"] = data.dict()
        dicts["update_time"] = str(datetime.datetime.now())
        save = SaveStepMongoModel[ZhongGuoQingNianBaoSave2](step_info=ZhongGuoQingNianBaoSave2()).parse_obj(
            dicts)
        result = await step_info.replace_one({'_id': ids}, save.dict(by_alias=True, exclude={"id"}))
    else:
        save = SaveStepMongoModel[ZhongGuoQingNianBaoSave2](_id=ids, product=product, types=types,
                                                            step_name=step_name,
                                                            step_info=ZhongGuoQingNianBaoSave2().parse_obj(
                                                                data.dict()),
                                                            update_time=str(datetime.datetime.now()),
                                                            create_time=str(datetime.datetime.now())
                                                            )
        result = await step_info.insert_one(save.dict(by_alias=True))
    return_info.status = SUCCESS
    return_info.msg_code = 200
    return_info.msg = "保存到mongodb成功"
    return_info.data = ""
    return return_info.todict()


@router.post("/step2_1/save_page")
async def parse_detail(input: InputInfoModel[ZhongGuoQingNianBaoSave3]):
    """
    保存到大表
    :param input:
    :return:
    """
    return_info = ReturnInfo()
    product = "ZQB"
    types = "11"
    step_name = "step2_1"
    data = input.data
    page = input.data.page
    name = input.data.periodDate
    ids = "_".join([product, types, step_name, name, page])
    # step_info = MongoDBClient.db30_1.client.htmljson.step_info
    step_info = Coll().get_step_info()
    cjip = await step_info.find_one({"_id": ids})
    if cjip:
        dicts = SaveStepMongoModel[ZhongGuoQingNianBaoSave3](step_info=ZhongGuoQingNianBaoSave3()).parse_obj(
            cjip).dict()
        dicts["step_info"] = data.dict()
        dicts["update_time"] = str(datetime.datetime.now())
        save = SaveStepMongoModel[ZhongGuoQingNianBaoSave3](step_info=ZhongGuoQingNianBaoSave3()).parse_obj(
            dicts)
        result = await step_info.replace_one({'_id': ids}, save.dict(by_alias=True, exclude={"id"}))
    else:
        save = SaveStepMongoModel[ZhongGuoQingNianBaoSave3](_id=ids, product=product, types=types,
                                                            step_name=step_name,
                                                            step_info=ZhongGuoQingNianBaoSave3().parse_obj(
                                                                data.dict()),
                                                            update_time=str(datetime.datetime.now()),
                                                            create_time=str(datetime.datetime.now())
                                                            )
        result = await step_info.insert_one(save.dict(by_alias=True))
    return_info.status = SUCCESS
    return_info.msg_code = 200
    return_info.msg = "保存到mongodb成功"
    return_info.data = ""
    return return_info.todict()


@router.post("/step4/save_article")
async def parse_detail(input: InputInfoModel[ZhongGuoQingNianBaoSave4]):
    """
    保存到大表
    :param input:
    :return:
    """
    return_info = ReturnInfo()
    product = "ZQB"
    types = "11"
    editions = "0"
    step_name = "step4"
    data = input.data
    name = input.data.rawid
    ids = "_".join([product, types, editions, step_name, name])
    # step_info = MongoDBClient.db30_1.client.htmljson.step_info
    step_info = Coll().get_paper()

    cjip = await step_info.find_one({"_id": ids})
    if cjip:
        dicts = SaveStepMongoModel[ZhongGuoQingNianBaoSave4](step_info=ZhongGuoQingNianBaoSave4()).parse_obj(
            cjip).dict()
        dicts["step_info"] = data.dict()
        dicts["update_time"] = str(datetime.datetime.now())
        save = SaveStepMongoModel[ZhongGuoQingNianBaoSave4](step_info=ZhongGuoQingNianBaoSave4()).parse_obj(
            dicts)
        result = await step_info.replace_one({'_id': ids}, save.dict(by_alias=True, exclude={"id"}))
    else:
        save = SaveStepMongoModel[ZhongGuoQingNianBaoSave4](_id=ids, product=product, types=types,
                                                            step_name=step_name,
                                                            step_info=ZhongGuoQingNianBaoSave4().parse_obj(
                                                                data.dict()),
                                                            update_time=str(datetime.datetime.now()),
                                                            create_time=str(datetime.datetime.now()),
                                                            editions=editions
                                                            )
        result = await step_info.insert_one(save.dict(by_alias=True))
    return_info.status = SUCCESS
    return_info.msg_code = 200
    return_info.msg = "保存到mongodb成功"
    return_info.data = ""
    return return_info.todict()


@router.post("/step4/save_article_parse_data")
async def parse_detail(input: InputInfoModel[ZhongGuoQingNianBaoParseSave4]):
    """
    保存到大表
    :param input:
    :return:
    """
    return_info = ReturnInfo()
    data = input.data.djson
    lngid = data["lngid"]
    data["_id"] = lngid
    step_info = Coll().get_paper_latest()
    cjip = await step_info.find_one({"_id": lngid})
    if cjip:
        result = await step_info.replace_one({'_id': lngid}, data)
    else:

        result = await step_info.insert_one(data)
    return_info.status = SUCCESS
    return_info.msg_code = 200
    return_info.msg = "保存到mongodb成功"
    return_info.data = ""
    return return_info.todict()
